package cn.bdqn.yan;

import cn.bdqn.yan.mapper.UserMapper;
import cn.bdqn.yan.pojo.Role;
import cn.bdqn.yan.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class Main {
    private static final Logger LOGGER = Logger.getLogger(String.valueOf(Main.class));

    public static void main(String[] args) {
        // write your code here
    }

    /**
     *根据用户名和角色id查询用户信息
     */
    @Test
    public void testgetuserlist() {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        try {
            sqlsession = MybatisUtil.createSqlSession();
            User user = new User();
            /*user.setUsername("小");
            user.setUserrole(2);*/
            String username = "明";
            Integer roleId = 0;
            list = sqlsession.getMapper(UserMapper.class).getuserlist(username, roleId);
            for (User users : list) {
                System.out.println(users.getUsername() + "\t" + users.getAddress());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
    }

    /**
     *根据用户id修改用户信息
     */
    @Test
    public void testmodify() {
        SqlSession sqlsession = null;
        int count = 0;
        try {
            sqlsession = MybatisUtil.createSqlSession();
            User user = new User();
            user.setId(3);
            user.setUsername("小明");
            user.setAddress("上海浦东区");
            count = sqlsession.getMapper(UserMapper.class).modify(user);
            //提交事务
            sqlsession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlsession.rollback();
            count = 0;
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
    }

    /**
     * 数组的方式传参
     * 根据用户角色列表。获取该角色列表下用户列表信息-foreach-array
     */
    @Test
    public void getUserByRoleId_foreach_array() {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        Integer roleids[] = {2, 3};
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list=sqlsession.getMapper(UserMapper.class).getUserByRoleId_foreach_array(roleids);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户角色Id\t用户地址");
        for (User user:list){
            System.out.println("  "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress());
        }
    }

    /**
     * list集合的方式传参
     * 根据用户角色列表查询用户信息
     */
    @Test
    public void getUserByRoleId_foreach_list() {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        List<Integer> rolelist =new ArrayList<Integer>();
        rolelist.add(2);
        rolelist.add(3);
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list=sqlsession.getMapper(UserMapper.class).getUserByRoleId_foreach_list(rolelist);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户角色Id\t用户地址");
        for (User user:list){
            System.out.println("  "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress());
        }
    }

    /**
     * Map的方式传参
     */
    @Test
    public void getUserByConditionMap_foreach_map() {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        Map<String,Object> conditionMap=new HashMap<String,Object>();
        List<Integer> rolelist =new ArrayList<Integer>();
        rolelist.add(2);
        rolelist.add(3);
        conditionMap.put("gender",1);
        conditionMap.put("roleIds",rolelist);
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list=sqlsession.getMapper(UserMapper.class).getUserByConditionMap_foreach_map(conditionMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户角色Id\t用户地址\t性别");
        for (User user:list){
            System.out.println("  "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
        }
    }

    /**
     *将list集合封装成Map方式作为参数，查询用户信息
     */
    @Test
    public void getUser_foreach_ByMap() {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        Map<String,Object> roleMap=new HashMap<String,Object>();
        List<Integer> rolelist =new ArrayList<Integer>();
        rolelist.add(2);
        rolelist.add(3);
        roleMap.put("rolekey",rolelist);
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list=sqlsession.getMapper(UserMapper.class).getUser_foreach_ByMap(roleMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户角色Id\t用户地址\t性别");
        for (User user:list){
            System.out.println("  "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
        }
    }

    /**
     * 根据多条件查询
     * @throws ParseException
     */
    @Test
    public void getUserList_choose() throws ParseException {
        SqlSession sqlsession = null;
        List<User> list = new ArrayList<User>();
        String username="";
        Integer userrole=2;
        String usercode="";
        //Date creationDate= (Date) new SimpleDateFormat("YYYY-MM-dd").parse("2018-2-10");
        String creationdate="2018-2-10";
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list=sqlsession.getMapper(UserMapper.class).getUserList_choose(username,userrole,usercode,creationdate);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户角色Id\t用户地址\t性别");
        for (User user:list){
            System.out.println("  "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
        }
    }

    /**
     * 分页查询
     * 以下共有三个list1,list2,list3分别代表不同的方法实现分页查询
     */
    @Test
    public void getUserListpage() {
        SqlSession sqlsession = null;
        List<Role> list1=new ArrayList<Role>();
        //List<Role> list2 = new ArrayList<Role>();
        //List<User> list3 = new ArrayList<User>();
        String username="";
        Integer userrole=2;
        Integer pagsize=3;
        Integer currentpageNo=0;
        try {
            sqlsession=MybatisUtil.createSqlSession();
            list1=sqlsession.getMapper(UserMapper.class).getUserListpage1(username,userrole,currentpageNo,pagsize);
            //list2=sqlsession.getMapper(UserMapper.class).getUserListpage2(username,userrole,currentpageNo,pagsize);
            //list3=sqlsession.getMapper(UserMapper.class).getUserListpage3(username,userrole,currentpageNo,pagsize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlsession);
        }
        System.out.println("姓名\t用户地址\t\t角色名称");
        //方法一
        List<Role> userList1=list1;
        for(Role role:userList1){
            for(int i=0;i<role.getUserList().size();i++){
                System.out.println(role.getUserList().get(i).getUsername()+"\t"+role.getUserList().get(i).getAddress()+"\t"+role.getRolename());
            }
        }
        //方法二
        /*List<User> userList2 = list2.get(0).getUserList();
        //List<Role> userrolelist=list2;
        for (User user:userList2){
            System.out.println("  "+user.getUsername() + "\t"+user.getAddress()+"\t"+list2.get(0).getRolename());
        }*/
        //方法三
        /*List<User> userList3=list3;
        for(User user:userList3){
            System.out.println("  "+user.getUsername() + "\t"+user.getAddress()+"\t"+user.getUserrolename());
        }*/
    }
}
